//
//  AllBaseViewController.m
//  bird_cloud_artist
//
//  Created by 闫珍 on 17/1/4.
//  Copyright © 2017年 dianchuang. All rights reserved.
//

#import "AllBaseViewController.h"

@interface AllBaseViewController ()

@end

@implementation AllBaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        if (Current_device_vesion >= 7.0) {
            if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]){
                self.automaticallyAdjustsScrollViewInsets = NO;
            }
        }
    }
    return self;
}

- (void)loadView {
    
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    view.backgroundColor = DefaultViewBackgroundColor;
    self.view = view;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    WS(weakSelf);
    //自定义导航
    _nav = [UIView new];
    _nav.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_nav];
    
    [_nav mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(weakSelf.view).insets(UIEdgeInsetsMake(0, 0, 0, 0));
        make.height.equalTo(@(NavHeight));
    }];
    
    _navBackView = [UIView new];
    _navBackView.backgroundColor = DefaultNavigationColor;
    [_nav addSubview:_navBackView];
    
    [_navBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_nav).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    _navTitle = [UILabel new];
    _navTitle.backgroundColor = [UIColor clearColor];
    _navTitle.textAlignment = NSTextAlignmentCenter;
    _navTitle.adjustsFontSizeToFitWidth = YES;
    _navTitle.font = [UIFont boldSystemFontOfSize:20];
    _navTitle.text = self.title;
    _navTitle.textColor = [UIColor whiteColor];
    [_nav addSubview:_navTitle];
    
    [_navTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(_nav).insets(UIEdgeInsetsMake(Status_Gap, 55, 0, 55));
        make.height.equalTo(@(NavHeight - Status_Gap));
    }];
    
    if(_canBack){
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, Status_Gap , 44, 44)];
        
        button.backgroundColor = [UIColor clearColor];
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
        [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(backPage:) forControlEvents:UIControlEventTouchUpInside];
        [_nav addSubview:button];
    }
    
    [self _iniView];
    
}

- (void)_iniView {
    
    if (_isHome) {
        _navBackView.alpha = 0.0f;
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

#pragma mark - MBHud
- (void)showHud:(NSString *)title isDim:(BOOL)isDim {
    _hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _hud.dimBackground = isDim;
    if(title.length > 0) {
        _hud.labelText = title;
    }
}

- (void)showHudComplete:(NSString *)title success:(BOOL)success {
    NSString *name = success ? @"success" : @"excla";
    _hud.dimBackground = NO;
    _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
    _hud.mode = MBProgressHUDModeCustomView;
    if(title.length > 0) {
        _hud.labelText = title;
    }
    NSTimeInterval seconds=success ? 1 : 2;
    
    [_hud hide:YES afterDelay:seconds];
}

- (void)ShowExclaHud:(NSString *)title{
    _hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self showHudComplete:title success:NO];
}

- (void)hideHud {
    [_hud hide:YES];
}

- (BOOL)checkCamera {
    BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    if(!isCamera) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"此设备没有摄像头" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return NO;
    }else{
        return YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)backPage:(id)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Public Method
- (UIButton *)createNavRightButton:(NSString *)showName target:(id)target action:(SEL)action
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(APP_SCREEN_WIDTH-60-5, Status_Gap, 60, CUSTOM_NAV_HEIGHT)];
    float fontsize=18.0f;
    if(showName.length>2){
        fontsize=16.0f;
    }
    button.titleLabel.font = [UIFont systemFontOfSize:fontsize];
    [button setTitle:showName forState:UIControlStateNormal];
    
    [button setBackgroundColor:[UIColor clearColor]];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [_nav addSubview:button];
    
    return button;
}

- (UIButton *)createNavRightButtonWithImage:(NSString *)imageString withFrame:(CGRect)frame target:(id)target action:(SEL)action
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(APP_SCREEN_WIDTH-20-frame.size.width, 0, frame.size.width+20, NavHeight)];
    [button setImage:[UIImage imageNamed:imageString] forState:UIControlStateNormal];
    
    button.imageEdgeInsets = UIEdgeInsetsMake(20, 0, 0, 0);
    [button setBackgroundColor:[UIColor clearColor ]];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [_nav addSubview:button];
    return button;
}


@end
